package org.languageguide.media;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.languageguide.media.FlashMediaGenerator;
import org.languageguide.media.HTML5MediaGenerator;
import org.languageguide.media.Mobile5MediaGenerator;
import org.languageguide.media.QuickTimeMediaGenerator;

public class MediaTagManager {
	
	public static final Config html5NoOffensive = new Config(new HTML5MediaGenerator(), false);
	public static final Config mobile5NoOffensive = new Config(new Mobile5MediaGenerator(), false);
	public static final Config flashNoOffensive = new Config(new FlashMediaGenerator(), false);
	public static final Config quicktimeNoOffensive = new Config(new QuickTimeMediaGenerator(), false);
	public static final Config html5Offensive = new Config(new HTML5MediaGenerator(), true);
	public static final Config flashOffensive = new Config(new FlashMediaGenerator(), false);
	public static final Config mobile5Offensive = new Config(new Mobile5MediaGenerator(), true);
	public static final Config quicktimeOffensive = new Config(new QuickTimeMediaGenerator(), true);

	
	public static String[] getIR2(Config config, String soundFile) {
		return config.getMediaGenerator().getIR2(soundFile);
	}
	
	public static String[] getIP2(Config config, String soundFile) {
		return config.getMediaGenerator().getIp2(soundFile);
	}

	
	public static Config getConfigFromCookie(HttpServletRequest req, HttpServletResponse res){
		Cookie[] cookies = req.getCookies();
		if (cookies != null){
			for (Cookie cookie: cookies){
				if (cookie.getName().equals("prefs2")){
					String value = cookie.getValue();
					int config = Integer.parseInt(value);
					switch (config){
						case 10: return flashNoOffensive;
						case 20: return html5NoOffensive;
						case 30: return mobile5NoOffensive;
						case 0: return quicktimeNoOffensive;
						case 11: return flashOffensive;
						case 21: return html5Offensive;
						case 31: return mobile5NoOffensive;
						case 1: return quicktimeOffensive;
					}
				}
			}
		}
		Config config;
		int value = useHTML5(req);
		if (value >= 1){
			if (value == 1)
				config = html5NoOffensive;
			else{
				//System.out.println("mobile no offensive");
				config = mobile5NoOffensive;
			}
		}
		else{
			config = flashNoOffensive;
		}
		setCookieFromConfig(res, config);
		return config;
	}
	
	
	private static void setCookieFromConfig(HttpServletResponse res, Config config){
		String value = config.getCookieString();
		Cookie cookie = new Cookie("prefs2", value);
		cookie.setMaxAge(-1);
		cookie.setPath("/");
		res.addCookie(cookie);
	}
		
	public static int useHTML5(HttpServletRequest request){
		String userAgent = request.getHeader("user-agent");
		if (userAgent != null){
			//System.out.println("" + userAgent);
			if (userAgent.indexOf("	WebKit") != -1){
				if (userAgent.indexOf("Chrome/") == -1){
					if (userAgent.indexOf("Mobile") != -1){
						return 2;
					}
					return 1;
				}
			}
			else if (userAgent.indexOf("MSIE 9") != -1){
				return 1;
			}
		}
		return 0;
	}
}

